package com.layblog.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Author 林必昭
 * @Date 2021/9/4 13:21
 */

@Configuration
@EnableWebSocketMessageBroker //注解表示开启使用STOMP协议来传输基于代理消息，Broker就是代理的意思
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 注册STOMP端点
     * addEndPoint：添加STOMP协议的端点，提供WebSocket获SockJS客户端访问的地址接口
     * withSockJS：使用withSockJS协议
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket").withSockJS();
    }

    /**
     * 配置消息代理
     * 启动Broker，当消息的发送地址符合配置的前缀消息才发送到这个broker
     *
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        registry.enableSimpleBroker("/user", "/topic");     //推送消息前缀
        registry.setApplicationDestinationPrefixes("/app");   //应用请求前缀
    }
}
